package cn.edu.sdu.sol.intake_system.repository;

import cn.edu.sdu.sol.intake_system.model.po.Question;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Repository
public interface QuestionRepository extends JpaRepository<Question, Integer> {

    @Query(value = "from Question where department.departmentId=?1")
    List<Question> findQuestionListByDepartmentId(Integer departmentId);


    @Query(value = "from Question where questionId=?1")
    Optional<Question> findQuestionByQuestionId(Integer questionId);

    @Query(value = "from Question where departmentQuestionId=?1")
    Optional<Question> findQuestionByDepartmentQuestionId(Integer departmentQuestionId);

    @Query(value = "from Question where departmentQuestionId=?1 and department.departmentId=?2")
    Optional<Question> findQuestionByDepartmentQuestionIdAndDepartmentId(Integer departmentQuestionId, Integer departmentId);

    @Transactional
    @Modifying
    @Query("DELETE FROM Question q WHERE q.department.departmentId = :departmentId")
    void deleteByDepartmentId(Integer departmentId);
}
